home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / ADVANCED / TESS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.5 KB  |  207 lines

  1.  
  2. /* tess.c - by David Blythe, SGI */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <GL/glut.h>
  7.  
  8. static GLfloat spin = 0;
  9. static int level = 4;
  10. static int model = 0;
  11. static GLfloat rotx, roty;
  12. static int ox = -1, oy = -1;
  13. static int mot;
  14. #define PAN     1
  15. #define ROT     2
  16.  
  17. void
  18. movelight(int x, int y) {
  19.     spin += (y-oy);
  20.     ox = x; oy = y;
  21.     if (spin > 360.) spin -= 360.;
  22.     if (spin < -360.) spin -= -360.;
  23.     glutPostRedisplay();
  24. }
  25.  
  26. void
  27. rotate(int x, int y) {
  28.     rotx += x-ox;
  29.     if (rotx > 360.) rotx -= 360.;
  30.     else if (rotx < -360.) rotx += 360.;
  31.     roty += y-oy;
  32.     if (roty > 360.) roty -= 360.;
  33.     else if (roty < -360.) roty += 360.;
  34.     ox = x; oy = y;
  35.     glutPostRedisplay();
  36. }
  37.  
  38. void
  39. motion(int x, int y) {
  40.     if (mot == PAN) movelight(x, y);
  41.     else if (mot == ROT) rotate(x,y);
  42. }
  43.  
  44. void
  45. mouse(int button, int state, int x, int y) {
  46.     if(state == GLUT_DOWN) {
  47.         switch(button) {
  48.         case GLUT_LEFT_BUTTON:
  49.             mot = PAN;
  50.             motion(ox = x, oy = y);
  51.             break;
  52.         case GLUT_MIDDLE_BUTTON:
  53.             mot = ROT;
  54.             motion(ox = x, oy = y);
  55.             break;
  56.         case GLUT_RIGHT_BUTTON:
  57.             break;
  58.         }
  59.     } else if (state == GLUT_UP) {
  60.         mot = 0;
  61.     }
  62. }
  63.  
  64. void togglewire(void) {
  65.     static int toggle = 0;
  66.     toggle ^= 1;
  67.     glPolygonMode(GL_FRONT_AND_BACK, toggle ? GL_LINE : GL_FILL);
  68. }
  69.  
  70. void genmodel(void) {
  71.     extern void sphere(int level);
  72.  
  73.     glNewList(1, GL_COMPILE);
  74.     if (model) {
  75.         GLUquadricObj *q = gluNewQuadric();
  76.  
  77.         gluSphere(q, 1.0, 10*level, 10*level);
  78.         gluDeleteQuadric(q);
  79.     } else {
  80.         sphere(level-1);
  81.     }
  82.     glEndList();
  83. }
  84.  
  85. void togglemodel(void) {
  86.     model ^= 1;
  87.     genmodel();
  88. }
  89.  
  90. void levelup(void) {
  91.     level += 1;
  92.     if (level > 7) level = 7;
  93.     genmodel();
  94. }
  95.  
  96. void leveldown(void) {
  97.     level -= 1;
  98.     if (level <= 0) level = 1;
  99.     genmodel();
  100. }
  101.  
  102. void help(void) {
  103.     printf("'h'      - help\n");
  104.     printf("'t'      - tessellation style\n");
  105.     printf("'UP'     - increase tessellation\n");
  106.     printf("'DOWN'   - decrease tessellation\n");
  107.     printf("left mouse     - rotate sphere\n");
  108.     printf("middle mouse   - move light\n");
  109. }
  110.  
  111. void init(void) {
  112.     GLfloat specular[4] = { 1., 1., 1., 1. };
  113.  
  114.     glEnable(GL_LIGHTING);
  115.     glEnable(GL_LIGHT0);
  116.     genmodel();
  117.     glDepthFunc(GL_LESS);
  118.     glEnable(GL_DEPTH_TEST);
  119.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
  120.     glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 30);
  121. }
  122.  
  123. void display(void) {
  124.     GLfloat position[] = { 0.0, 0.0, 3.5, 1.0 };
  125.  
  126.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  127.     glPushMatrix();
  128.     glTranslatef(0.0, 0.0, -5.0); 
  129.  
  130.     glPushMatrix();
  131.     glRotatef(spin, 1.0, 0.0, 0.0);
  132.     glRotatef(0.0, 1.0, 0.0, 0.0);
  133.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  134.  
  135.     glPopMatrix();
  136.  
  137.     glRotatef(rotx, 0., 1., 0.);
  138.     glRotatef(roty, 1., 0., 0.);
  139.     glCallList(1);
  140.     glPopMatrix();
  141.     glutSwapBuffers();
  142. }
  143.  
  144. void reshape(int w, int h) {
  145.     glViewport(0, 0, w, h);
  146.     glMatrixMode(GL_PROJECTION);
  147.     glLoadIdentity();
  148.     gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
  149.     glMatrixMode(GL_MODELVIEW);
  150. }
  151.  
  152. /* ARGSUSED1 */
  153. void
  154. key(unsigned char key, int x, int y) {
  155.     switch(key) {
  156.     case 't': togglemodel(); break;
  157.     case 'w': togglewire(); break;
  158.     case 'h': help(); break;
  159.     case '\033': exit(0);
  160.     default: break;
  161.     }
  162.     glutPostRedisplay();
  163. }
  164.  
  165. /* ARGSUSED1 */
  166. void
  167. special(int key, int x, int y) {
  168.     switch(key) {
  169.     case GLUT_KEY_UP:   levelup(); break;
  170.     case GLUT_KEY_DOWN: leveldown(); break;
  171.     }
  172.     glutPostRedisplay();
  173. }
  174.  
  175. void
  176. menu(int value)
  177. {
  178.     if(value<0)
  179.       special(-value,0,0);
  180.     else
  181.        key((unsigned char) value,0,0);
  182. }
  183.  
  184. int main(int argc, char** argv) {
  185.     glutInit(&argc, argv);
  186.     glutInitWindowSize(512, 512);
  187.     glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
  188.     (void)glutCreateWindow("Quality of sphere tesselation");
  189.     init();
  190.     glutDisplayFunc(display);
  191.     glutKeyboardFunc(key);
  192.     glutSpecialFunc(special);
  193.     glutReshapeFunc(reshape);
  194.     glutMouseFunc(mouse);
  195.     glutMotionFunc(motion);
  196.     glutCreateMenu(menu);
  197.     glutAddMenuEntry("Toggle sphere model", 't');
  198.     glutAddMenuEntry("Toggle solid/wireframe", 'w');
  199.     glutAddMenuEntry("Increase tessellation", -GLUT_KEY_UP);
  200.     glutAddMenuEntry("Decrease tessellation", -GLUT_KEY_DOWN);
  201.     glutAddMenuEntry("Print help message", 'h');
  202.     glutAddMenuEntry("Quit", '\033');
  203.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  204.     glutMainLoop();
  205.     return 0;             /* ANSI C requires main to return int. */
  206. }
  207.